using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
    /*
      var qKDSTAMM = from m in mtemp.KDSTAMM select m;
       DBMapper.MapKDSTAMM(qKDSTAMM.ToList(), true);
       */
        public List<KDSTAMM> MapKDSTAMM(XElement item2, bool mergedb)
        {
            List<KDSTAMM> result = new List<KDSTAMM>();
            var item4 = from item3 in item2.Elements()
                        select new KDSTAMM
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            KDMANDANT = (string)item3.Element("KDMANDANT"),
                            KDKUNDNR = (string)item3.Element("KDKUNDNR"),
                            KDKUNDNA = (string)item3.Element("KDKUNDNA"),
                            KDABCKL = (string)item3.Element("KDABCKL"),
                            KDNAME1 = (string)item3.Element("KDNAME1"),
                            KDKNAME = (string)item3.Element("KDKNAME"),
                            KDNAME2 = (string)item3.Element("KDNAME2"),
                            KDSTRASSE1 = (string)item3.Element("KDSTRASSE1"),
                            KDSTRASSE2 = (string)item3.Element("KDSTRASSE2"),
                            KDTEL = (string)item3.Element("KDTEL"),
                            KDMAILADR = (string)item3.Element("KDMAILADR"),
                            PHOTO = (string)item3.Element("PHOTO")
                        }; 
                        
          result = item4.ToList();
          MapKDSTAMM(result, mergedb);
          return result;
        }
        ///DBMapper.MapKDSTAMM(mtemp.KDSTAMM.ToList<KDSTAMM>(), true);        
       public void MapKDSTAMM(List<KDSTAMM> result, bool mergedb)
        {
          if (mergedb)
            {     
            
                int loaded = 0;
                int page = 500;
                int count = result.Count();
                while (true)
                {
                    if (count - loaded < page) 
                    {
                        page = count - loaded;
                    }
                    var query =
                        (from KDSTAMM row in result.AsQueryable()
                         select row).Skip(loaded).Take(page);
               var rowsexist = from u in LocalDB.KDSTAMM.ToList().AsQueryable()
                                           join uc in query.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.KDSTAMM.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.KDSTAMM.InsertAllOnSubmit(query);
                LocalDB.SubmitChanges();
                 loaded += page;
                if (count == loaded)
                        break;
                }
            }
       }
    }
    [Table(Name = "KDSTAMM")]
    public class KDSTAMM : TBBASE
    {
        private string _MAINKEY = "0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    _MAINKEY = value;
                    NotifyPropertyChanging("MAINKEY");                 
                }
            }
        }
        private string _Photo = "YES";
        [Column]
        public string PHOTO
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    _Photo = value;
                    NotifyPropertyChanging("PHOTO");
                }
            }
        } 
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _KDMANDANT="0";
        [Column]
        public string KDMANDANT
        {
            get
            {
                return _KDMANDANT;
            }
            set
            {
                if (value != _KDMANDANT)
                {
                     _KDMANDANT = value;
                    NotifyPropertyChanged("KDMANDANT");
                }
            }
        }
        private string  _KDKUNDNR="0";
        [Column]
        public string KDKUNDNR
        {
            get
            {
                return _KDKUNDNR;
            }
            set
            {
                if (value != _KDKUNDNR)
                {
                     _KDKUNDNR = value;
                    NotifyPropertyChanged("KDKUNDNR");
                }
            }
        }
        private string  _KDKUNDNA="0";
        [Column]
        public string KDKUNDNA
        {
            get
            {
                return _KDKUNDNA;
            }
            set
            {
                if (value != _KDKUNDNA)
                {
                     _KDKUNDNA = value;
                    NotifyPropertyChanged("KDKUNDNA");
                }
            }
        }
        private string  _KDABCKL="0";
        [Column]
        public string KDABCKL
        {
            get
            {
                return _KDABCKL;
            }
            set
            {
                if (value != _KDABCKL)
                {
                     _KDABCKL = value;
                    NotifyPropertyChanged("KDABCKL");
                }
            }
        }
        private string  _KDNAME1="0";
        [Column]
        public string KDNAME1
        {
            get
            {
                return _KDNAME1;
            }
            set
            {
                if (value != _KDNAME1)
                {
                     _KDNAME1 = value;
                    NotifyPropertyChanged("KDNAME1");
                }
            }
        }
        private string  _KDKNAME="0";
        [Column]
        public string KDKNAME
        {
            get
            {
                return _KDKNAME;
            }
            set
            {
                if (value != _KDKNAME)
                {
                     _KDKNAME = value;
                    NotifyPropertyChanged("KDKNAME");
                }
            }
        }
        private string  _KDNAME2="0";
        [Column]
        public string KDNAME2
        {
            get
            {
                return _KDNAME2;
            }
            set
            {
                if (value != _KDNAME2)
                {
                     _KDNAME2 = value;
                    NotifyPropertyChanged("KDNAME2");
                }
            }
        }
        private string  _KDSTRASSE1="0";
        [Column]
        public string KDSTRASSE1
        {
            get
            {
                return _KDSTRASSE1;
            }
            set
            {
                if (value != _KDSTRASSE1)
                {
                     _KDSTRASSE1 = value;
                    NotifyPropertyChanged("KDSTRASSE1");
                }
            }
        }
        private string  _KDSTRASSE2="0";
        [Column]
        public string KDSTRASSE2
        {
            get
            {
                return _KDSTRASSE2;
            }
            set
            {
                if (value != _KDSTRASSE2)
                {
                     _KDSTRASSE2 = value;
                    NotifyPropertyChanged("KDSTRASSE2");
                }
            }
        }
        private string  _KDTEL="0";
        [Column]
        public string KDTEL
        {
            get
            {
                return _KDTEL;
            }
            set
            {
                if (value != _KDTEL)
                {
                     _KDTEL = value;
                    NotifyPropertyChanged("KDTEL");
                }
            }
        }
        private string  _KDMAILADR="0";
        [Column]
        public string KDMAILADR
        {
            get
            {
                return _KDMAILADR;
            }
            set
            {
                if (value != _KDMAILADR)
                {
                     _KDMAILADR = value;
                    NotifyPropertyChanged("KDMAILADR");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<KDSTAMM> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\KDSTAMM\\KDSTAMM" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\KDSTAMM" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("KDSTAMM");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (KDSTAMM row in ListInput)
                {
                    XElement f = new XElement("KDSTAMM");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement KDMANDANT = new XElement("KDMANDANT");
                         KDMANDANT.Value = row.KDMANDANT;
                          f.Add(KDMANDANT);
                        XElement KDKUNDNR = new XElement("KDKUNDNR");
                         KDKUNDNR.Value = row.KDKUNDNR;
                          f.Add(KDKUNDNR);
                        XElement KDKUNDNA = new XElement("KDKUNDNA");
                         KDKUNDNA.Value = row.KDKUNDNA;
                          f.Add(KDKUNDNA);
                        XElement KDABCKL = new XElement("KDABCKL");
                         KDABCKL.Value = row.KDABCKL;
                          f.Add(KDABCKL);
                        XElement KDNAME1 = new XElement("KDNAME1");
                         KDNAME1.Value = row.KDNAME1;
                          f.Add(KDNAME1);
                        XElement KDKNAME = new XElement("KDKNAME");
                         KDKNAME.Value = row.KDKNAME;
                          f.Add(KDKNAME);
                        XElement KDNAME2 = new XElement("KDNAME2");
                         KDNAME2.Value = row.KDNAME2;
                          f.Add(KDNAME2);
                        XElement KDSTRASSE1 = new XElement("KDSTRASSE1");
                         KDSTRASSE1.Value = row.KDSTRASSE1;
                          f.Add(KDSTRASSE1);
                        XElement KDSTRASSE2 = new XElement("KDSTRASSE2");
                         KDSTRASSE2.Value = row.KDSTRASSE2;
                          f.Add(KDSTRASSE2);
                        XElement KDTEL = new XElement("KDTEL");
                         KDTEL.Value = row.KDTEL;
                          f.Add(KDTEL);
                        XElement KDMAILADR = new XElement("KDMAILADR");
                         KDMAILADR.Value = row.KDMAILADR;
                          f.Add(KDMAILADR);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }                                        
    }
}
